

function request(that, params, sCallback, eCallback){
	url = apiUrl + params.url;
	if(!params.data) {
		params.data = {};
	}
	if(!token){
		token = localStorage.getItem('token');
	}
	params.data.token = token;

	params.data.mobileType = 'h5';	
	params.data.merid = merid;
	that.$http.post(url,params.data,{emulateJSON : true}).then(function(res){
		layer.closeAll();
		var res = res.body;
		console.log('请求成功');
		// console.log(JSON.stringify(res));
		sCallback && sCallback(res);
	},function(error){
		console.log('请求失败');
		console.log(JSON.stringify(error));
		if(eCallback) {
			eCallback(error);
		} else {
			layer.closeAll();
			mui.toast(error.body.msg);
		}
	});
}

function ajaxRequest(params, sCallback, eCallback){

	if(!params) {
		params = {};
	}
	var url = "";
	var data = {};
	if (isOldServer) {
		url = oldServerApiUrl + params.path;
		data = Object.assign(params.data || {}, mustParams);
	} else {
		url = apiUrl + params.path;
		params.data.token = token;
		params.data.mobileType = 'h5';
		params.data.merid = merid;
		data = params.data;
	}

	$.ajax({
		type: params.type || "GET",
		url: url,
		data: data,
		dataType: "JSON",
		// contentType: 'application/json',
		success: function (ret) {
			layer.closeAll();
			console.log(ret);
			var res = ret;
			if (typeof ret.code === 'undefined') {
				res = ret.data;
			}
			console.log('请求成功');
			console.log(JSON.stringify(res));
			sCallback && sCallback(res);
		},
		error: function (error) {
			console.log('请求失败');
			console.log(JSON.stringify(error));
			if(eCallback) {
				eCallback(error.data);
			} else {
				layer.closeAll();
				mui.toast(error.data.msg);
			}
		}
	});
}


function execFunc(fn, params, mode) {
	let mod = mode || 'get';
	let result = null;
	switch (fn) {
		case 'json':
			result =  mod == 'get' ? JSON.parse(params) : JSON.stringify(params)
			break;
		case 'urlencode':
			result =  mod == 'get' ? decodeURIComponent(decodeURIComponent(params)) : encodeURIComponent(params)
			break;
		case 'escape':
			result =  mod == 'get' ? unescape(params) : escape(params)
			break;
		default:
			break;
	}
	return result;
}

function getParam(pname, isFun) {
	var funcArray = isFun ? ['escape', 'urlencode', 'json'] : ['escape'];
	var  reg = new  RegExp("(^|&)" + pname + "=([^&]*)(&|$)");
	var  r = window.location.search.substr(1).match(reg);
	if(r != null) {
		var result = r[2];
		for (var i = 0; i < funcArray.length; i++) {
			var func = funcArray[i];
			result = execFunc(func, result);
		}
		return result;
	}
	return  null;
}

function setParam(value, isFun) {
	var funcArray = isFun === 1 ? [ 'json', 'urlencode', 'escape'] : ['escape'];
	var result = value;
	for (var i = 0; i < funcArray.length; i++) {
		var func = funcArray[i];
		result = execFunc(func, result, 'set');
	}
	return result;
}
